package com.sxhalo.fsms.controller.api;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.sxhalo.fsms.domain.*;
import com.sxhalo.fsms.service.IUserPatrolItemRecordService;
import com.sxhalo.fsms.util.CookieUtils;
import com.sxhalo.fsms.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IDeviceDefectRecordService;
import com.sxhalo.fsms.util.ApiResult;

@Controller
@RequestMapping("/api")
public class DeviceDefectRecordController extends BaseController {

	@Autowired
	private IDeviceDefectRecordService recService;
	@Autowired
	private IUserPatrolItemRecordService recordService;


	@ResponseBody
	@RequestMapping(value = "/defectrecord", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findPatrol(Integer typeId,Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "1=1";
		String order = "create_time desc";
		if (!isBlank(req.getParameter("areaName"))) {
			where += " and area_id = " + req.getParameter("areaName").trim();
		}
		if (!isBlank(req.getParameter("deviceName"))) {
			String sel = " select device_id from fact_device where device_name like '%" + req.getParameter("deviceName").trim() + "%'";
			where += " and device_id in (" + sel + ")";
		}
		if (!isBlank(req.getParameter("defect"))) {
			where += " and defect like '%" + req.getParameter("defect").trim() + "%'";
		}
		if (!isBlank(req.getParameter("defectSource"))) {
			where += " and defect_source = '" + req.getParameter("defectSource").trim() + "'";
		}
		return findList(current, pageSize, where, order, recService);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "新增设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult savePatrol(@RequestBody DeviceDefectRecord record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
//		Integer maxId = recService.getMaxId();
//		record.setDefectId(maxId + 1);
		record.setCreateTime(nowtime);
		record.setUpdateTime(nowtime);
		recService.insertSelective(record);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "巡检记录转缺陷", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord/convert", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult savePatrol(DeviceDefectRecord record,Integer jobId,Integer jobItemId, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());

		record.setCreateTime(nowtime);
		record.setUpdateTime(nowtime);

		UserPatrolItemRecordKey itemkey=new UserPatrolItemRecordKey();
		itemkey.setJobId(jobId);
		itemkey.setJobItemId(jobItemId);
		itemkey.setRecordId(record.getJobRecordId());
		UserPatrolItemRecord item=recordService.selectByPrimaryKey(itemkey);
		item.setIsConvert(1);
		record.setFindTime(item.getCreateTime());
		recService.insertSelective(record);
		recordService.updateByPrimaryKeySelective(item);

		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "更新设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updatePatrol(@RequestBody DeviceDefectRecord record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		recService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "删除设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delPatrol(Integer defectId, HttpServletRequest req) {
		recService.deleteByPrimaryKey(defectId);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "批量删除设备缺陷记录", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord/batchdel", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delPatrol(String ids, HttpServletRequest req) {
		recService.deleteByWhere(" defect_id in ("+ids+")");
		return ApiResult.retOK(req);
	}



	@ResponseBody
	@SysControllerLog(operationName = "消缺", modelName = "设备缺陷记录")
	@RequestMapping(value = "/defectrecord/rectification", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult conformPatrol(@RequestBody DeviceDefectRecord record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		String token = CookieUtils.getCookieValue(req, "token");
		if (token != null) {
			Integer userId = TokenUtil.getUserId(token);
			record.setDefectEliminationPerson(userId);
		}
		record.setDefectEliminationTime(nowtime);
		recService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}
}
